package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.macro.mall.mapper.UmsMemberStatisticsInfoMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberStatisticsInfo;
import com.macro.mall.model.UmsMemberStatisticsInfoExample;
import com.macro.mall.portal.domain.MemberProductCollection;
import com.macro.mall.portal.repository.MemberProductCollectionRepository;
import com.macro.mall.portal.service.MemberCollectionService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员收藏Service实现类
 * Created by macro on 2020/7/31.
 */
@Service
public class MemberCollectionServiceImpl implements MemberCollectionService {
    @Autowired
    private MemberProductCollectionRepository productCollectionRepository;
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private UmsMemberStatisticsInfoMapper memberStatisticsInfoMapper;
    @Override
    public int add(MemberProductCollection productCollection) {
        int count = 0;
        UmsMember member = memberService.getCurrentMember();
        productCollection.setMemberId(member.getId());
        productCollection.setMemberNickname(member.getNickname());
        productCollection.setMemberIcon(member.getIcon());
        MemberProductCollection findCollection = productCollectionRepository.findByMemberIdAndProductId(productCollection.getMemberId(), productCollection.getProductId());
        if (findCollection == null) {
            productCollectionRepository.save(productCollection);
            UmsMemberStatisticsInfoExample memberStatisticsInfoExample = new UmsMemberStatisticsInfoExample();
            memberStatisticsInfoExample.createCriteria().andMemberIdEqualTo(member.getId());
            List<UmsMemberStatisticsInfo> umsMemberStatisticsInfos = memberStatisticsInfoMapper.selectByExample(memberStatisticsInfoExample);
            if (CollUtil.isNotEmpty(umsMemberStatisticsInfos)){
                UmsMemberStatisticsInfo memberStatisticsInfo = umsMemberStatisticsInfos.get(0);
                memberStatisticsInfo.setCollectProductCount(memberStatisticsInfo.getCollectProductCount()+1);
                memberStatisticsInfoMapper.insert(memberStatisticsInfo);
            }
            count = 1;
        }
        return count;
    }

    @Override
    public int delete(List<Long> productIds) {
        UmsMember member = memberService.getCurrentMember();
        int i=0;
        for (Long productId:productIds){
            productCollectionRepository.deleteByMemberIdAndProductId(member.getId(), productId);
            i++;
        }
        UmsMemberStatisticsInfoExample memberStatisticsInfoExample = new UmsMemberStatisticsInfoExample();
        memberStatisticsInfoExample.createCriteria().andMemberIdEqualTo(member.getId());
        List<UmsMemberStatisticsInfo> umsMemberStatisticsInfos = memberStatisticsInfoMapper.selectByExample(memberStatisticsInfoExample);
        if (CollUtil.isNotEmpty(umsMemberStatisticsInfos)){
            UmsMemberStatisticsInfo memberStatisticsInfo = umsMemberStatisticsInfos.get(0);
            memberStatisticsInfo.setCollectProductCount(memberStatisticsInfo.getCollectProductCount()-i);
            memberStatisticsInfoMapper.insert(memberStatisticsInfo);
        }
        return i;
    }

    @Override
    public Page<MemberProductCollection> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return productCollectionRepository.findByMemberId(member.getId(), pageable);
    }

    @Override
    public MemberProductCollection detail(Long productId) {
        UmsMember member = memberService.getCurrentMember();
        return productCollectionRepository.findByMemberIdAndProductId(member.getId(), productId);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        productCollectionRepository.deleteAllByMemberId(member.getId());
        UmsMemberStatisticsInfoExample memberStatisticsInfoExample = new UmsMemberStatisticsInfoExample();
        memberStatisticsInfoExample.createCriteria().andMemberIdEqualTo(member.getId());
        List<UmsMemberStatisticsInfo> umsMemberStatisticsInfos = memberStatisticsInfoMapper.selectByExample(memberStatisticsInfoExample);
        if (CollUtil.isNotEmpty(umsMemberStatisticsInfos)){
            UmsMemberStatisticsInfo memberStatisticsInfo = umsMemberStatisticsInfos.get(0);
            memberStatisticsInfo.setCollectProductCount(0);
            memberStatisticsInfoMapper.insert(memberStatisticsInfo);
        }
    }
}
